package com.lhf.novel.sysuser.security.config;

import com.lhf.novel.sysuser.security.*;
import com.lhf.novel.sysuser.service.SysUserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * SecurityConfig实体类
 *
 * @author zy 刘会发
 * @since 2020/3/9$
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private SysUserService sysUserService;
    //    @Resource
//    private BCryptPasswordEncoder passwordEncoder;
    @Resource
    private SuccessHandler successHandler;
    @Resource
    private FailureHandler failureHandler;
    @Resource
    private DeniedHandler deniedHandler;
    @Resource
    private EntryPoint entryPoint;
    @Resource
    private DataSource dataSource;
    @Resource
    private NLogoutSuccessHandler logoutSuccessHandler;

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        // 配置数据源
        jdbcTokenRepository.setDataSource(dataSource);
//        jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(sysUserService).passwordEncoder(bCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll()
                .and().formLogin().loginProcessingUrl("/login").successHandler(successHandler).failureHandler(failureHandler)
                .and().logout().logoutSuccessHandler(logoutSuccessHandler).logoutUrl("/logout").permitAll()
                .and().exceptionHandling().accessDeniedHandler(deniedHandler).authenticationEntryPoint(entryPoint)
                .and().csrf().disable()
//                .and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .cors().disable()
                .rememberMe().rememberMeParameter("rememberMe").tokenRepository(persistentTokenRepository()).tokenValiditySeconds(2 * 60 * 60)
                .and().sessionManagement().maximumSessions(1)
        ;
    }
}
